package cn.kust.clock.controller;

import cn.kust.clock.pojo.dto.LikeDTO;
import cn.kust.clock.service.LikeService;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author YL
 * @data 2022/7/26 14:46
 **/
@Slf4j

@RestController
@RequestMapping("/api-clock/like")
public class LikeController {

    @Autowired
    LikeService likeService;



    /**
     * 根据传入的type_id, type, user_id 添加一条点赞记录
     * @param likeDTO 点赞详细DTO类
     * @return 受影响行数
     */
    @PostMapping("/insert")
    public ResponseResult insertLikeHistory(@RequestBody LikeDTO likeDTO) {
        log.info("进入添加一条点赞记录Controller");
        if (likeDTO.getUserId() == null) {
            return ResponseResult.fail(10086,"userId为空！");
        }
        if (likeDTO.getType() == null) {
            return ResponseResult.fail(10086,"type为空！");
        }
        if (likeDTO.getType() != 1 && likeDTO.getType() != 2) {
            return ResponseResult.fail(10086,"类型不正确！");
        }
        if (likeDTO.getTypeId() == null) {
            return ResponseResult.fail(10086,"typeId为空！");
        }
        if (likeDTO.getStatus() == null) {
            return ResponseResult.fail(10086,"status为空！");
        }
        Integer count = likeService.insertLikeHistory(likeDTO);

        if (count == 1) {
            return ResponseResult.success("插入成功");
        }

        return ResponseResult.fail(10086, "插入失败");
    }

    /**
     * 删除一条点赞记录，即status设为0
     * @param id 点赞主键id
     * @return 受影响行数
     */
    @GetMapping("/delete/{id}")
    public ResponseResult deleteLikeHistoryById(@PathVariable("id") Long id) {
        log.info("进入删除点赞记录Controller");
        Integer count = likeService.deleteLikeHistoryById(id);

        if (count == 1) {
            return ResponseResult.success("删除成功");
        }

        return ResponseResult.fail(10086, "删除失败");
    }


    /**
     * 根据点赞id与点赞状态修改点赞状态
     * @param likeDTO 包括type typeId userId status
     * @return 受影响行数
     */
    @PostMapping("/update")
    public ResponseResult updateLikeHistoryByLikeDTO(@RequestBody LikeDTO likeDTO){
        log.info("进入修改点赞记录Service");
        if (likeDTO.getUserId() == null) {
            return ResponseResult.fail(10086,"userId为空！");
        }
        if (likeDTO.getType() == null) {
            return ResponseResult.fail(10086,"type为空！");
        }
        if (likeDTO.getType() != 1 && likeDTO.getType() != 2) {
            return ResponseResult.fail(10086,"类型不正确！");
        }
        if (likeDTO.getTypeId() == null) {
            return ResponseResult.fail(10086,"typeId为空！");
        }
        if (likeDTO.getStatus() == null) {
            return ResponseResult.fail(10086,"status为空！");
        }
        Integer count = likeService.updateLikeHistoryByLikeDTO(likeDTO);

        if (count == 1) {
            return ResponseResult.success("更新成功");
        }

        return ResponseResult.fail(10086, "更新失败");
    }
}
